home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / chmagic / chmagic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-30  |  3.3 KB  |  163 lines

  1. /* 
  2.  * chmagic.c --
  3.  *
  4.  *    Change the magic number of a BSD format demand paged executable.
  5.  *
  6.  * Copyright 1990 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header$";
  18. #endif /* not lint */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #include <a.out.h>
  25. #include <sys/exec.h>
  26. #include <sys/file.h>
  27.  
  28. extern int errno;
  29.  
  30. static int forceFlag;
  31.  
  32. static int changeMagic _ARGS_ ((int magic, const char *filename));
  33. static int getMagic _ARGS_ ((const char *magicString));
  34. static void usage _ARGS_ ((void));
  35.  
  36. /*
  37.  *----------------------------------------------------------------------
  38.  *
  39.  * main --
  40.  *
  41.  *
  42.  *
  43.  * Results:
  44.  *    None.
  45.  *
  46.  * Side effects:
  47.  *    None.
  48.  *
  49.  *----------------------------------------------------------------------
  50.  */
  51.  
  52. void
  53. main(argc, argv)
  54.     int argc;
  55.     char **argv;
  56. {
  57.     int i;
  58.     int magic;
  59.     int status;
  60.  
  61.     if (argc < 3) {
  62.     usage();
  63.     }
  64.     if (strcmp(argv[1], "-f") == 0) {
  65.     forceFlag = 1;
  66.     ++argv;
  67.     --argc;
  68.     }
  69.     magic = getMagic(argv[1]);
  70.     status = EXIT_SUCCESS;
  71.     for (i = 2; i < argc; ++i) {
  72.     if (changeMagic(magic, argv[i])) {
  73.         status = EXIT_FAILURE;
  74.     }
  75.     }
  76.     exit(status);
  77. }
  78.  
  79. static int
  80. getMagic(magicString)
  81.     const char *magicString;
  82. {
  83.     int i;
  84.     int base;
  85.     char *endPtr;
  86.     int val;
  87.     static struct {
  88.     const char *string;
  89.     int val;
  90.     } tab[] = {
  91.     { "OMAGIC",           OMAGIC },
  92.     { "NMAGIC",           NMAGIC },
  93.     { "ZMAGIC",           ZMAGIC },
  94.     { "SPRITE_ZMAGIC",    SPRITE_ZMAGIC },
  95.     { "UNIX_ZMAGIC",      UNIX_ZMAGIC },
  96.     };
  97.  
  98.     /*
  99.      * See if the magicString specifies on of the magic symbols
  100.      */
  101.     for (i = 0; i < sizeof(tab)/sizeof(*tab); ++i) {
  102.     if (strcmp(magicString, tab[i].string) == 0) {
  103.         return tab[i].val;
  104.     }
  105.     }
  106.     val = strtoul(magicString, &endPtr, 0);
  107.     if (*endPtr != '\0') {
  108.     usage();
  109.     }
  110.     return val;
  111. }
  112.  
  113. static int
  114. changeMagic(magic, filename)
  115.     int magic;
  116.     const char *filename;
  117. {
  118.     int fd;
  119.     struct exec buf;
  120.     int r;
  121.  
  122.     if ((fd = open(filename, O_RDWR)) < 0) {
  123.     (void) fprintf(stderr, "Cannot open %s: %s\n",
  124.         filename, strerror(errno));
  125.     return 1;
  126.     }
  127.  
  128.     if ((r = read(fd, (char *) &buf, sizeof(buf))) != sizeof(buf)) {
  129.     if (r < 0) {
  130.         (void) fprintf(stderr, "Error reading %s: %s\n",
  131.         filename, (r < 0) ? strerror(errno) : "file too small");
  132.     }
  133.     return 1;
  134.     }
  135.     if (forceFlag == 0 && N_BADMAG(buf)) {
  136.     (void) fprintf(stderr, "Bad magic number for %s: 0x%x\n",
  137.         filename, buf.a_magic);
  138.     return 1;
  139.     }
  140.     buf.a_magic = magic;
  141.     if (lseek(fd, 0L, L_SET) != 0) {
  142.     (void) fprintf(stderr, "Error seeking %s: %s\n",
  143.         filename, strerror(errno));
  144.     return 1;
  145.     }
  146.     if (write(fd, (char *) &buf, sizeof(buf)) != sizeof(buf)) {
  147.     (void) fprintf(stderr, "Error writing %s: %s\n",
  148.         filename, strerror(errno));
  149.     return 1;
  150.     }
  151.     close(fd);
  152.     return 0;
  153. }
  154.  
  155. static void
  156. usage()
  157. {
  158.  
  159.     (void) fprintf(stderr, "usage: chmagic [-f] MAGIC file1 ...\n");
  160.     exit(EXIT_FAILURE);
  161. }
  162.  
  163.